//
//  ZCScanDeatailsMoreVC.m
//  zctx
//
//  Created by apple on 16/3/17.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCScanDeatailsMoreVC.h"
#import "ZCScandetailsVC.h"
#import "ZCScanProblemVC.h"
#import "ZCScanChooseVC.h"
#import "LGSegment.h"
#import "ZCSearchCell.h"
#import "ZCSearchModel.h"
#import "ZCRaiseVC.h"

#import "ZCScanVC.h"
#import "ZCScanErrorcorrectVC.h"
#import "ZCLoginVC.h"
//Segment高度
#define LG_segmentH 38
#define HeadViewH 92

@interface ZCScanDeatailsMoreVC ()<UIScrollViewDelegate,SegmentDelegate>

@property (nonatomic, strong) UIScrollView *contentScrollView;
@property(nonatomic,strong)NSMutableArray *buttonList;
@property (nonatomic, weak) LGSegment *segment;
@property(nonatomic,weak)CALayer *LGLayer;

@property(nonatomic,strong)ZCSearchModel *scanModel;

@property(nonatomic,strong)ZCScanProblemVC *vc3;

@end

@implementation ZCScanDeatailsMoreVC

- (NSMutableArray *)buttonList
{
    if (!_buttonList)
    {
        _buttonList = [NSMutableArray array];
    }
    return _buttonList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self initnavegation];
    self.view.backgroundColor = [UIColor whiteColor];
    self.scanModel = [ZCSearchModel modelObjectWithDictionary:self.barcodeDictionary];
    
    [self addHeadView];
    //加载Segment
    [self setSegment];
    //加载ViewController
    [self addChildViewController];
    //加载ScrollView
    [self setContentScrollView];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    ProgressDismiss;
    
    if (self.intput ==1) {
        for (UIViewController *temp in self.navigationController.viewControllers) {
            if ([temp isKindOfClass:[ZCScanVC class]]) {
                [self.navigationController popToViewController:temp animated:YES];
            }
        }
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addHeadView{
    
    if (self.barcodeDictionary.count>0) {
        ZCSearchCell *cell = [[ZCSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"csd" cellType:ZCSearchCellTypeTitle];
        cell.contentView.frame = CGRectMake(0,0,SCREEN_WIDTH, HeadViewH);
        cell.titleLable.text = self.scanModel.name;
        
        
        NSString *url =[NSString stringWithFormat:@"http://%@",self.scanModel.classify_img];
        cell.imgurl = url;

        [self.view addSubview:cell.contentView];
        
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickErrorBtn)];
        [cell.constView addGestureRecognizer:recognizer];
        
        [cell.faqiBtn addTarget:self action:@selector(addTapGesture) forControlEvents:UIControlEventTouchUpInside];
        [cell.errorBtn addTarget:self action:@selector(clickErrorBtn) forControlEvents:UIControlEventTouchUpInside];
    }else{
        ZCSearchCell *cell = [[ZCSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"nocsd" cellType:ZCSearchCellTypeNothing];
        cell.contentView.frame = CGRectMake(0,0,SCREEN_WIDTH, HeadViewH);
        [self.view addSubview:cell.contentView];
    }
}

#pragma mark 发起检测
-(void)addTapGesture{
    ZCRaiseVC * raisevc = [[ZCRaiseVC alloc]init];
    raisevc.hidesBottomBarWhenPushed = YES;
    raisevc.pushpjName = self.scanModel.name;
    [self.navigationController pushViewController:raisevc animated:YES];
}

#pragma mark 我要纠错
-(void)clickErrorBtn{
    NSLog(@"我要纠错");
    if (UserModel.password.length>1) {
        ZCScanErrorcorrectVC *errorVC = [[ZCScanErrorcorrectVC alloc]init];
        errorVC.hidesBottomBarWhenPushed = YES;
        errorVC.titleStr = self.scanModel.name;
        errorVC.barcodeStr = self.barcodeStr;
        [self.navigationController pushViewController:errorVC animated:YES];
    }else{
        ZCLoginVC *login = [[ZCLoginVC alloc]init];
        login.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:login animated:YES];
    }
}

-(void)setSegment {
    
    [self buttonList];
    //初始化
    LGSegment *segment = [[LGSegment alloc]initWithFrame:CGRectMake(0, HeadViewH, SCREEN_WIDTH, LG_segmentH)];
    segment.backgroundColor = [UIColor whiteColor];
    segment.delegate = self;
    self.segment = segment;
    [self.view addSubview:segment];
    [self.buttonList addObject:segment.buttonList];
    self.LGLayer = segment.LGLayer;
}

//加载ScrollView的颜色
-(void)setContentScrollView {
 
    UIScrollView *sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, LG_segmentH+HeadViewH, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:sv];
    sv.bounces = NO;
    sv.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    sv.contentOffset = CGPointMake(0, 0);
    sv.pagingEnabled = YES;
    sv.showsHorizontalScrollIndicator = NO;
    sv.scrollEnabled = YES;
    sv.userInteractionEnabled = YES;
    sv.delegate = self;
    
//    for (int i=0; i<self.childViewControllers.count; i++) {
//        UIViewController * vc = self.childViewControllers[i];
//        vc.view.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//        [sv addSubview:vc.view];
//        
//    }
    
    UIViewController *vc = [self.childViewControllers firstObject];
    vc.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [sv addSubview:vc.view];
    
    sv.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
    self.contentScrollView = sv;
}
//加载3个ViewController
-(void)addChildViewController{
    
    ZCScandetailsVC * vc1 = [[ZCScandetailsVC alloc]init];
    vc1.barcodeDictionary = self.barcodeDictionary;
    vc1.barStr = self.barcodeStr;
    [self addChildViewController:vc1];
    ZCScanChooseVC * vc2 = [[ZCScanChooseVC alloc]init];
    vc2.dataChooseDict = self.buyDictionary;
    [self addChildViewController:vc2];
    self.vc3 = [[ZCScanProblemVC alloc]init];
    self.vc3.dataproblemDict = self.faqDictionary;
    [self addChildViewController:self.vc3];
    
    
}
#pragma mark - UIScrollViewDelegate
//实现LGSegment代理方法
-(void)scrollToPage:(int)Page {
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = SCREEN_WIDTH * Page;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentScrollView.contentOffset = offset;
        [self scrollViewDidEndScrollingAnimation:self.contentScrollView];
    }];
}
// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetX = scrollView.contentOffset.x;
    [self.segment moveToOffsetX:offsetX];
}

/** 滚动结束（手势导致） */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 获得索引
    NSUInteger index = scrollView.contentOffset.x / self.contentScrollView.frame.size.width;
    // 添加控制器
    UIViewController *newsVc = self.childViewControllers[index];
    
    newsVc.view.frame = CGRectMake(index * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//    if (newsVc.view.superview) return;
//    newsVc.view.frame = scrollView.bounds;
    [self.contentScrollView addSubview:newsVc.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
